package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //搜索框模糊查询
        //构建查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //构建分页条件
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //根据name分页查询菜品信息列表
         page = dishMapper.selectPage(page, dishLambdaQueryWrapper);

         //遍历结果
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            //根据菜品的category_id从分类表查询分类名称
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());


            //根据菜品的id从口味表查询到当前菜品的口味信息
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
            dish.setFlavors(dishFlavors);//封装当前菜品的口味列表
        }
        return page;
    }

    //同一个业务中，发送多条增删改语句的时候，一定要考虑事务问题
    @Transactional
    @Override
    public void save(Dish dish) {
        //执行添加sql语句,回显id
        dishMapper.insert(dish);

        //将dish中的口味列表，遍历保存到口味表
        List<DishFlavor> flavors = dish.getFlavors();
        //判断是否为空
        if (CollectionUtil.isNotEmpty(flavors)) {
            //遍历
            for (DishFlavor flavor : flavors) {
                //赋值
                flavor.setDishId(dish.getId());
                //添加语句
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Transactional
    @Override
    public Dish findById(Long id) {
        //根据id查询
        Dish dish = dishMapper.selectById(id);
        //根据菜品id查询口味
        //创建条件
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        //查询
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
        //封装
        dish.setFlavors(dishFlavors);//封装当前菜品口味列表
        return dish;
    }

    @Transactional
    @Override
    public void update(Dish dish) {
        //根据主键修改
        dishMapper.updateById(dish);
        //通过id删除口味列表
        //创建条件
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        //删除
        dishFlavorMapper.delete(dishFlavorLambdaQueryWrapper);

        //保存新的口味列表
        List<DishFlavor> flavors = dish.getFlavors();
        //判断是否为空
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                //赋值
                flavor.setDishId(dish.getId());
                //保存
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Transactional
    @Override
    public void deleteByIds(Long[] ids) {
//            //切割“,”
//            String[] idList = ids.split(",");
            //遍历数组
        //判断要删除的闪频是否全部停售
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(dishLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("要删除的菜品还在售状态，不能删除");
        }
        //遍历
        for (Long id : ids) {
            //根据菜品id删除菜品的口味列表
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(dishFlavorLambdaQueryWrapper);

            //根据菜品id删除菜品信息
            dishMapper.deleteById(id);
        }
    }


    @Transactional
    @Override
    public void updateByStatus(Long[] ids, Integer sales) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(sales);
            dishMapper.updateById(dish);
        }
    }


    @Override
    public List<Dish> findList(Long categoryId, Integer status, String name) {
        //创建查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(categoryId != null,Dish::getCategoryId, categoryId);
        dishLambdaQueryWrapper.eq(Dish::getStatus, status);

        //模糊查询条件
        dishLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //执行查询
        List<Dish> dishList = dishMapper.selectList(dishLambdaQueryWrapper);

        //遍历
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //得到每个菜品，根据菜品的id从口味表中找到当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }


}
